package com.lin.service.impl;

import com.lin.entity.Menu;
import com.lin.mapper.MenuMapper;
import com.lin.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.utils.MenuTreeBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Lin
 * @since 2022-03-01
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Menu> findMenuTree() {
        List<Menu> menus = this.baseMapper.selectList(null);
        List<Menu> menuList = MenuTreeBuilder.build(menus);
        return menuList;
    }

    private List<Menu> getChild(Long id, List<Menu> rootMenu) {
        List<Menu> childList = new ArrayList<>();
        for (Menu menu : rootMenu) {
            /**
             * 不是0
             * 类型不是1
             * 父级id必须要和传过来的父级id相等
             */
            if(!menu.getParentId().equals("0")&&!menu.getType().equals(id)){
                childList.add(menu);
            }
        }
        //三级以及三级以上的菜单使用递归传递实现
        for (Menu menu: childList) {
            menu.setChildMenus(getChild(menu.getId(),rootMenu));
        }
        //递归退出条件
        if(childList.size()==0){
            return null;
        }
        return childList;
    }
}
